'   Copyright (C) 2011  Jan-Willem de Bruyn

'   This program is free software: you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation, either version 3 of the License, or
'   (at your option) any later version.
'
'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with this program.  If not, see <http://www.gnu.org/licenses/>. 

Imports System.IO
Imports System.Net
Imports System.Configuration

Public Class psyftp
    Public Shared Function list_ftp(ByVal ftp_adress As String, ByVal ftp_username As String, ByVal ftp_password As String, ByVal ftp_path As String) As ArrayList
        'lists directory from ftp_adress with the credentials: ftp_username and ftp_password and the path as string and then returns an array of directory
        'if fails there is a messagebox with the error and returns an empty arraylist
        Dim ftpreq As FtpWebRequest = WebRequest.Create(New Uri(ftp_adress & ftp_path))
        ftpreq.Method = WebRequestMethods.Ftp.ListDirectory
        ftpreq.Credentials = New NetworkCredential(ftp_username, ftp_password)
        ftpreq.UseBinary = True
        Dim arr As ArrayList = New ArrayList
        Try
            Dim sr As New StreamReader(ftpreq.GetResponse.GetResponseStream())
            Dim str As String = sr.ReadLine()

            While Not str Is Nothing
                arr.Add(str)
                str = sr.ReadLine()
            End While
            sr.Close()
            sr = Nothing
            ftpreq = Nothing
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        Return arr
    End Function

    Public Shared Function download_ftp(ByVal ftp_adress As String, ByVal ftp_username As String, ByVal ftp_password As String, ByVal ftp_path As String, ByVal ftp_file As String) As Stream
        'download file with the given ftp_adress as ftp adress, ftp_username as ftp username, ftp_password as ftp password, ftp_path as ftp path and ftp_file as the file to download.
        'if fails there is a messagebox with the error and returns an empty stream
        Dim ftpreq As FtpWebRequest = WebRequest.Create(New Uri(ftp_adress & ftp_path & ftp_file))
        ftpreq.Method = WebRequestMethods.Ftp.DownloadFile
        ftpreq.Credentials = New NetworkCredential(ftp_username, ftp_password)
        ftpreq.UseBinary = True
        Dim response As Stream = Nothing
        Try
            response = ftpreq.GetResponse.GetResponseStream

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        Return response
    End Function

    Public Shared Function upload_ftp(ByVal ftp_adress As String, ByVal ftp_username As String, ByVal ftp_password As String, ByVal ftp_path As String, ByVal ftp_file As Stream, ByVal ftp_filename As String) As Boolean
        'upload the ftp_file as stream with the filename: ftp_filename and the creditials ftp_username as username, ftp_password as ftp password on the ftp_adress as ftp adress and ftp_path as the ftp path.
        'if fails there is a messagebox with the error and returns false
        Dim ftpreq As FtpWebRequest = WebRequest.Create(New Uri(ftp_adress & ftp_path & ftp_filename))
        ftpreq.Method = WebRequestMethods.Ftp.UploadFile
        ftpreq.Credentials = New NetworkCredential(ftp_username, ftp_password)
        ftpreq.UseBinary = True
        Dim buffer() As Byte = Nothing
        Try
            ftp_file.Read(buffer, 0, ftp_file.Length)
            ftpreq.GetRequestStream().Write(buffer, 0, buffer.Length)
            ftpreq = Nothing
            Return (True)
        Catch ex As Exception
            MsgBox(ex.ToString)
            Return False
        End Try

    End Function
End Class

Public Class psyfile
    Public Shared Function file_multicopy(ByVal file_source As String, ByVal file_destination As ArrayList) As Boolean
        '@ <summary>
        '@ copies the file_source as file source to the file_destination as a array with the file destinations as string
        '@ if fails there is a messagebox with the error and returns false
        '@ </summary>
        Try
            For Each Str As String In file_destination
                FileCopy(file_source, Str)
            Next
            Return True
        Catch ex As Exception
            MsgBox(ex.ToString)
            Return False
        End Try
    End Function

    Public Shared Function file_multimove(ByVal file_source As String, ByVal file_destination As ArrayList) As Boolean
        'moves the file_source as file source path to the file_destination as a array with the file destination path as string
        'if fails there is a messagebox with the error and returns false
        Try
            For Each Str As String In file_destination
                FileCopy(file_source, Str)
            Next
            FileSystem.Kill(file_source)
            Return True
        Catch ex As Exception
            MsgBox(ex.ToString)
            Return False
        End Try
    End Function

    Public Shared Function file_multidelete(ByVal file_files As ArrayList) As Boolean
        'deletes the files in the file_files array as file path string
        'if fails there is a messagebox with the error and returns false
        Try
            For Each Str As String In file_files
                Kill(Str)
            Next
            Return True
        Catch ex As Exception
            MsgBox(ex.ToString)
            Return False
        End Try
    End Function
    Public Shared Function file_multicheck(ByVal file_source As ArrayList) As ArrayList
        'checks if file_source as file path exists and returns an array with true or false values
        'if fails there is a messagebox with the error and returns an empty arraylist
        Dim arr As ArrayList = New ArrayList
        Try
            For Each Str As String In file_source
                arr.Add(IO.File.Exists(Str))
            Next
            Return arr
        Catch ex As Exception
            MsgBox(ex.ToString)
            Return arr
        End Try

    End Function

    Public Shared Function file_multicreate(ByVal file_destination As ArrayList) As Boolean
        Try
            For Each Str As String In file_destination
                IO.File.Create(Str)
            Next
            Return True
        Catch ex As Exception
            MsgBox(ex.ToString)
            Return False
        End Try

    End Function

End Class

Public Class bpms
    Public Shared Function write_xml(ByVal xmlfile_name As String, ByVal xmlfile_path As String, ByVal input_text As String, ByVal input_element As String, Optional ByVal xmlfile_after As String = "")
        Dim writer As New Xml.XmlTextWriter(xmlfile_path & xmlfile_name, Nothing)
        Try

            If Not xmlfile_after.ToString = "" Then
                writer.LookupPrefix(xmlfile_after)
                writer.WriteStartElement(input_element)
                writer.WriteString(input_text)
                writer.WriteEndElement()
                writer.Flush()
                writer.Close()
            Else
                writer.WriteStartDocument()
                writer.WriteStartElement(input_element)
                writer.WriteString(input_text)
                writer.WriteEndElement()
                writer.WriteEndDocument()
                writer.Flush()
                writer.Close()
            End If
            Return True
        Catch ex As Exception
            MsgBox(ex.ToString)
            writer.Close()
            Return False
        End Try


    End Function
    
End Class
